<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <script>
    var str = "    abc  def  ghi jkl  mn  "
    //循环去空格
    var stt = "";
    for (var i = 0; i < str.length; i++) {
      if (str[i] != " ") {
        stt += str[i];
      }
    }
    str = stt;
    console.log(str);

    //正则表达式去空格
    var str = "    abc  def  ghi jkl  mn  ";
    str = str.replace(/\s+/g, "");
    console.log(str);

    //正则案例 练习 查看编辑器如何使用正则
    console.log("moon2xyz".replace(/o/,"ab"));//mabon2xyz
    console.log("moon2xyz".replace(/o/g,"ab"));//mababn2xyz
    console.log("moon2 ooxyz".replace(/\bo/g,"ab"));//moon2 aboxyz
    console.log("moon2xyz".replace(/\dx/,"_"));//moon_yz
    console.log("moon2xyz".replace(/[xyz]/g,"ab"));//moon2ababab
    console.log("m2on2x2z".replace(/\d[zo]/g,"ab"));//mabn2xab
    console.log("m2on2x2z".replace(/2[x-z]/g,""));//m2on

    //将下列文章中 单独的大写C（前后没有字符）统一改为大写D，要求其他的c不受影响
    
    var str = "Chaude and Cold A patron in Montreal cafe turned on a tap in the washroom and got scalded. This is an outrage, he complained. The faucet marked C gave me boiling water.But, Monsieur, C stands for chaude - French for hot. You should know that if you live in Montreal.Wait a minute, roared the patron. The other tap is also marked C.Of course, said the manager, It stands for cold. After all, Montreal is a bilingual city."
    var reg = /C(?=\W)/mg;
    var newStr = str.replace(reg,"D");

    // 正则测试工具
    // http://www.iteye.com/news/29859
    // https://rubular.com/r/xfQHocREGj

    //常见正则案例
    // https://blog.csdn.net/liangqibinde/article/details/79205585

    // 参考文档
    // https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Regular_Expressions
  </script>
</body>

</html>